package com.jizhang.controller;


import com.jizhang.base.model.BaseModel;
import com.jizhang.entity.User;
import com.jizhang.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 郭博民
 * @since 2022-07-14
 */
@RestController
@RequestMapping("/user")
public class UserController {
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
    @Autowired
    UserService userService;

    //根据用户账号查询
    @ApiOperation(value = "根据用户账号查询信息", httpMethod = "GET")
    @GetMapping("/findUser")
    public BaseModel findUser(String username, BaseModel baseModel) throws Exception {
        System.out.println("执行查询用户操作"+df.format(new Date()));
        System.out.println("username:"+username);
        return userService.findUser(username, baseModel);
    }


    //添加用户 注册
    @ApiOperation(value = "添加(注册)用户", httpMethod = "POST")
    @PostMapping("/addUser")
    public BaseModel addUser(@RequestBody User user, BaseModel baseModel) throws Exception {
        System.out.println("执行添加用户操作"+df.format(new Date()));
        System.out.println("user:"+user);
        return userService.addUser(user, baseModel);
    }

    //更新用户信息
    @ApiOperation(value = "更新用户信息", httpMethod = "PUT")
    @PutMapping("/updateUser")
    public BaseModel updateUser(@RequestBody User user, BaseModel baseModel) throws Exception {
        System.out.println("更新后的用户数据:" + user);
        return userService.updateUser(user,baseModel);
    }

    // 用户登录
    @ApiOperation(value = "用户登录", httpMethod = "POST")
    @PostMapping("/loginUser")
    public BaseModel loginUser(@RequestBody User user, BaseModel baseModel) throws Exception {
        System.out.println("用户登录"+df.format(new Date()));
        System.out.println("user:"+user);
        return userService.loginUser(user, baseModel);
    }
}
